<?php

class Comment_model extends CI_Model{

	function __construct()
    {
        parent::__construct();
	}
	
	function get_all_comment($num, $offset, $kind=0)
	{
		if($kind) $this->db->where('fm_comment.cm_kind', $kind);
		$this->db->select('lb_email,fm_comment.*') ;
		$this->db->order_by('comment_id','DESC');
		$this->db->join('fm_member','fm_member.member_id=fm_comment.member_id','left') ;
		$this->db->where('fm_comment.bl_active !=', -1);
		return $this->db->get('fm_comment',$num,$offset)->result();
	}
	
	function get_num_comment($kind=0)
	{
		if($kind) $this->db->where('fm_comment.cm_kind', $kind);
		$this->db->where('fm_comment.bl_active !=', -1);
		$this->db->join('fm_member','fm_member.member_id=fm_comment.member_id','left') ;
		return $this->db->get('fm_comment')->num_rows();          
	}
	
	function trash($id)
	{
		$this->db->where('comment_id',$id);
		if($this->db->update('fm_comment',array('bl_active'=>-1))){
			return true;
		}else{
			return false;
		}
	}
	
	function remove()
	{
		$date = new DateTime();
		$date->modify('-3 month');
		$date_del = $date->format('Y-m-d H:i:s');
		
		$this->db->where('bl_active', -1);
		$this->db->where('dt_create <', $date_del);
		if($this->db->delete('fm_comment')){
		  	return true;
		}else{
		  	return false;
		}
	}
	
	function saveData($id,$data)
	{
		 if($id != 0){
			  $this->db->where('comment_id',$id);
			  if($this->db->update('fm_comment',$data))
			  {
					return true;
			  }else{
					return false;
			  }
		 }else{

			  if($this->db->insert('fm_comment',$data))
			  {
					return true;
			  }else{
					return false;
			  }
		 }          
	}
	
	function get_comment($id=0)
	{
		 $this->db->where('comment_id',$id);
		 return $this->db->get('fm_comment')->row();
	}
	
	function get_list_zone()
	{
		 $this->db->order_by('lb_zone','ASC');
		 return $this->db->get('fm_zone')->result();
	}
}
